import { Provide } from '@midwayjs/core';
import { Orders } from '../entity/Orders';
import { InjectEntityModel } from '@midwayjs/orm';
import { Equal, Not, Repository } from 'typeorm';
import { BaseService } from './base';
import { TeamLeaderServiceFee } from '../entity/TeamLeaderServiceFee';
import { ExpertCommission } from '../entity/ExpertCommission';

@Provide()
export class OrdersService extends BaseService<Orders> {
  @InjectEntityModel(Orders)
  entity: Repository<Orders>;

  async add(data) {
    return await super.add(data);
  }
  async upsert(data) {
    return await super.upsert(data, { conflictPaths: ['order_number', 'id'] });
  }
  async update(data) {
    return await super.update(data);
  }
  async list({ data, ...otherData }) {
    return await super.list({
      data: { ...data, status: Not(Equal('已关闭')) },
      otherData: { ...otherData },
    });
  }
  async page({ data, ...otherData }) {
    return await super.page({
      data: { ...data, status: Not(Equal('已关闭')) },
      otherData,
    });
  }
  async all(data: any = {}) {
    console.log(data);

    return await this.entity
      .createQueryBuilder('orders')
      .where('orders.data_date = :data_date', {
        data_date: data.data_date,
      })
      .andWhere('orders.store_name = :store_name', {
        store_name: data.store_name,
      })
      .andWhere('orders.status != "已关闭"')
      .leftJoinAndMapOne(
        'orders.teamLeaderServiceFee',
        TeamLeaderServiceFee,
        'teamLeaderServiceFee',
        'teamLeaderServiceFee.product_id = orders.product_id'
      )
      .leftJoinAndMapOne(
        'orders.expertCommission',
        ExpertCommission,
        'expertCommission',
        'expertCommission.product_id = orders.product_id'
      )
      .getMany();
  }
}
